using System;
using System.Xml;


namespace XmlProvider
{
    /// <summary>
    /// Using for Web.Config
    /// </summary>
    public class XmlProvider
    {
        private string errorMessage = "";
        public string ErrorMessage
        {
            set { errorMessage = value; }
            get { return errorMessage; }
        }
        
        private string xmlFilePath = "Web.Config";
        public string XmlFilePath
        {
            set { xmlFilePath = value; }
            get { return xmlFilePath; }
        }

        private string databaseName = "RecruitVietnam"; 
        public string DatabaseName
        {
            set { databaseName = value; }
            get { return databaseName; }
        }
         
                       
        string ReadNodeValue(string nodeName)
        {
            string nodeValue = "";
            try
            {
               
                bool foundNode = false;
                XmlDocument xmlDocument = new XmlDocument();

                xmlDocument.Load(xmlFilePath);

                XmlNodeList xmlNodeList =
                xmlDocument.GetElementsByTagName(nodeName);
                foreach (XmlNode xmlNode in xmlNodeList)
                {                 
                    foreach (XmlElement xmlElement in
                    xmlNode.ChildNodes)
                    {
                        if(xmlElement.LocalName == "add" &&
                        xmlElement.Attributes[
                        "name"].Value == databaseName)
                        {
                            foundNode = true;
                            nodeValue = xmlElement.Attributes[
                            "connectionString"].Value;
                            break;
                        }
                    }
                    if(foundNode) break;
                }
                
            }
            catch(Exception ex)
            {
                errorMessage = ex.Message;
            }
            return nodeValue;
        }
        
        void DeleteNodeName(string nodeName)
        {
            
            try
            {
                bool foundNode = false;
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlFilePath);
                XmlNodeList xmlNodeList =
                xmlDocument.GetElementsByTagName(nodeName);
                foreach (XmlNode xmlNode in xmlNodeList)
                {
                    foreach (XmlNode xmlChildNode in
                        xmlNode.ChildNodes)
                    {
                        if (xmlChildNode.LocalName == "add" &&
                        xmlChildNode.Attributes[
                        "name"].Value == databaseName)
                        {
                            foundNode = true;
                            xmlNode.RemoveChild(xmlChildNode);
                            break;
                        }
                    }
                    if(foundNode) 
                        break;                 
                }
                xmlDocument.Save(xmlFilePath);
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
            
        }
        
        void AddNewNode(string nodeName, string nameValue,
        string attributeName, string attributeValue)
        {
            try
            {
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(xmlFilePath);
                XmlElement newElement =
                xmlDocument.CreateElement("add");
                newElement.SetAttribute("name", 
                nameValue);
                newElement.SetAttribute(
                attributeName, attributeValue);
                xmlDocument.DocumentElement[
                nodeName].AppendChild(newElement);
                xmlDocument.Save(xmlFilePath);
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }
        }
    }
}
